package org.dromara.agriculture.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.agriculture.domain.vo.AgricultureBaseinfoVo;
import org.dromara.agriculture.domain.bo.AgricultureBaseinfoBo;
import org.dromara.agriculture.service.IAgricultureBaseinfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 基地信息
 *
 * @author Robin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/agriculture/baseinfo")
public class AgricultureBaseinfoController extends BaseController {

    private final IAgricultureBaseinfoService agricultureBaseinfoService;

    /**
     * 查询基地信息列表
     */
    @SaCheckPermission("agriculture:baseinfo:list")
    @GetMapping("/list")
    public TableDataInfo<AgricultureBaseinfoVo> list(AgricultureBaseinfoBo bo, PageQuery pageQuery) {
        return agricultureBaseinfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出基地信息列表
     */
    @SaCheckPermission("agriculture:baseinfo:export")
    @Log(title = "基地信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AgricultureBaseinfoBo bo, HttpServletResponse response) {
        List<AgricultureBaseinfoVo> list = agricultureBaseinfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "基地信息", AgricultureBaseinfoVo.class, response);
    }

    /**
     * 获取基地信息详细信息
     *
     * @param baseId 主键
     */
    @SaCheckPermission("agriculture:baseinfo:query")
    @GetMapping("/{baseId}")
    public R<AgricultureBaseinfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long baseId) {
        return R.ok(agricultureBaseinfoService.queryById(baseId));
    }

    /**
     * 新增基地信息
     */
    @SaCheckPermission("agriculture:baseinfo:add")
    @Log(title = "基地信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AgricultureBaseinfoBo bo) {
        return toAjax(agricultureBaseinfoService.insertByBo(bo));
    }

    /**
     * 修改基地信息
     */
    @SaCheckPermission("agriculture:baseinfo:edit")
    @Log(title = "基地信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AgricultureBaseinfoBo bo) {
        return toAjax(agricultureBaseinfoService.updateByBo(bo));
    }

    /**
     * 删除基地信息
     *
     * @param baseIds 主键串
     */
    @SaCheckPermission("agriculture:baseinfo:remove")
    @Log(title = "基地信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] baseIds) {
        return toAjax(agricultureBaseinfoService.deleteWithValidByIds(List.of(baseIds), true));
    }
}
